<?php
namespace Home\Controller;


class MagazineController extends BaseController
{

    public function _initialize()
    {
        parent::_initialize();
        $cat = C('CAT');
        $this->assign('category', $cat);
    }

    public function add()
    {
        if (IS_POST) {
            $magazineModel = D('Magazine');
            if ($magazineModel->create()) {
                if ($magazineModel->add()) {
                    $this->success('添加成功');
                } else {
                    $this->error($magazineModel->getDbError());
                }
            } else {
                $this->error($magazineModel->getError());
            }
        } else {
            $this->assign('action', U('Magazine/add'));
            $this->display();
        }
    }

    public function edit()
    {
        if (IS_POST) {
            $magazineModel = D('Magazine');
            if ($magazineModel->create()) {
                if ($magazineModel->save()) {
                    $this->success('修改成功');
                } else {
                    $this->error($magazineModel->getDbError());
                }
            } else {
                $this->error($magazineModel->getError());
            }
        } else {
            $mzId = I('GET.id', 0, 'intval');
            if($mzId == 0) $this->error('参数错误', U('Magazine/lists'));
            $this->assign('action', U('Magazine/edit'));
            $info = D('Magazine')->where(array('mz_id' => $mzId))->find();
            if($info){
                $this->assign('info', $info);
                $this->display('Magazine/add');
            }else{
                $this->error('不存在此期刊', U('Magazine/lists'));
            }
        }
    }
    
    public function lists()
    {
        if(IS_AJAX){
            $pageNow = I('POST.start', 0, 'intval');
            $pageSize = I('POST.length', 10, 'intval');
            $magazineModel = D('Magazine');
            $rowCount = $magazineModel->where(array('mz_status' => 1))->count();
            $lists = $magazineModel->where(array('mz_status' => 1))->order('mz_id DESC')->page($pageNow * $pageSize, $pageSize)->select();
            foreach ($lists as &$list) {
                $list['DT_RowId'] = $list['mz_id'];
            }

            $this->ajaxReturn(array('recordsTotal' => $rowCount, 'recordsFiltered' => $rowCount, 'data' => $lists, 'draw' => I('POST.draw')));
        }else{
            $this->display();
        }
    }
    
    public function del(){
        $id = I('GET.id', 0, 'intval');
        $magazineModel = D('Magazine');
        $info = $magazineModel->find($id);
        if($info){
            $data = array('mz_id' => $id, 'mz_status' => 0, 'mz_lasttime' => time());
            if($magazineModel->save($data)){
                $this->success('删除成功！');
            }else{
                $this->error('操作失败'. $magazineModel->_sql());
            }
        }else{
            $this->error('期刊不存在');
        }
    }

    public function modify()
    {
        sleep(2);
        $this->ajaxReturn(array(), 0);
    }

}